<template>
  <div>
    <!-- 弹框表单 -->
    <!-- 
        el-dialog
       visible	是否显示 Dialog，支持 .sync 修饰符	boolean
       itle	Dialog 的标题，也可通过具名 slot （见下表）传入 
       before-close	关闭前的回调，会暂停 Dialog 的关闭	function(done)，done 用于关闭 Dialog
       center	是否对头部和底部采用居中布局	boolean
     -->
    <el-dialog
      :title="isAdd ? '添加会员列表' : '修改会员列表'"
      :visible.sync="isShow"
      center
      :before-close="cancel"
    >
      <el-form ref="menu" :model="memberinfo" :rules="rules">
        <el-form-item
          prop="title"
          label="手机号"
          :label-width="formLabelWidth"
        >
          <el-input v-model="memberinfo.phone" autocomplete="off"></el-input>      
        </el-form-item>

 <el-form-item
          prop="title"
          label="昵称"
          :label-width="formLabelWidth"       >
          <el-input v-model="memberinfo.title" autocomplete="off"></el-input>           
        </el-form-item>
        
         <el-form-item
          prop="title"
          label="密码"
          :label-width="formLabelWidth"
        >
          <el-input v-model="memberinfo.password" autocomplete="off"></el-input>
        </el-form-item>留空则不修改

        <!-- 
          当选择目录的时候只能出现菜单图标 否则就要出现菜单地址
         -->
      
       
        <el-form-item label="状态" :label-width="formLabelWidth">
          <!-- 
                el-switch
                active-value	switch 打开时的值
                inactive-value	switch 关闭时的值
                active-color	switch 打开时的背景色
                inactive-color	switch 关闭时的背景色
             -->
          <el-switch
            v-model="memberinfo.status"
            active-color="#06c"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="isAdd" type="primary" @click="editInfo"
          >修改</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
//引入封装好的菜单接口
import { getMemberList,addlist,} from "../../request/api";
//引入辅助性函数
import { mapActions, mapGetters } from "vuex";
//引入封装好的，路由地址信息
import { indexRoutes } from "../../router";
export default {
  data() {
    return {
      fileList:[],
      memberinfo: {
        title:'',
        phone:'',
        password:'',
        status: 1, //状态   1正常2禁用
      },
      //验证规则
      rules: {
        title: [
          { required: true, message: "请输入手机号名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
        pid: [{ required: true, message: "请输入要修改的昵称", trigger: "change" }],
      },
      formLabelWidth: "120px",
      //创建一个图标数组
      iconlist: [
        "el-icon-s-tools",
        "el-icon-setting",
        "el-icon-s-goods",
        "el-icon-goods",
      ],
      indexRoutes: indexRoutes,
    };
  },
  computed: {
    ...mapGetters({
      menulist: "goods/getBannerList",
    }),
  },
  props: ["isShow", "isAdd"],
  methods: {
    onChange(value){
      this.memberinfo.img=value.raw;
      console.log(value.raw);
    },
    //封装一个change事件用来实现目录和菜单以及选择菜单的联动
    changePid(){
      this.memberinfo.type = this.memberinfo.pid==0?1:2
    },
    ...mapActions({ getMenu: "goods/getbannerListAction" }),
    //封装一个关闭弹框事件
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    //封装一个清空事件
    reset() {
      this.memberinfo = {
       uid:'',
        nickname:'',
        phone:'',
        password:'',
        status: 1, //状态   1正常2禁用
      };
      //   resetFields	对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      //清空表单验证
      this.$refs.menu.resetFields();
    },
    //封装一个菜单添加
    addInfo() {
      this.$refs.menu.validate((val) => {
        if (val) {
          // console.log(this.memberinfo,'输入表单');
          //调用菜单添加接口
        
      console.log(this.memberinfo)
          banneradd(this.memberinfo).then((res) => {
            // console.log(res,'菜单添加');
            if (res.code == 200) {
              this.$message.success(res.msg);
              //关闭弹框
              this.cancel();
              //重新调用列表接口
              this.getMenu();
              // location.reload()
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          console.log("验证失败");
        }
      });
    },
    //查询一条数据方法
    lookup(id) {
      // console.log(id,'列表的id');
     getMemberList({ id }).then((res) => {
        console.log(res, "查询一条数据");
        if (res.code == 200) {
          this.memberinfo = res.list;
          this.fileList=[]
          //给编辑表单对象添加id
          this.fileList.push({
            url:this.memberinfo.img
          })
          this.memberinfo.id = id;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    //封装一个菜单编辑
    editInfo() {
      this.$refs.menu.validate((val) => {
        if (val) {
          // console.log(this.memberinfo,'输入表单');
          //调用菜单编辑接口
          postBannerCate(this.memberinfo).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              //关闭弹框
              this.cancel();
              //重新调用列表接口
              this.getMenu();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          console.log("验证失败");
        }
      });
    },
  },
};
</script>

<style scoped></style>
